import Vue from 'vue'
import VueRouter from 'vue-router'
import { Loading, Message } from 'element-ui'

const routes = [
  {
    path: '/',
    redirect: '/index',
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/home/Index'),
  },
  {
    path: '/class',
    name: 'class',
    component: () => import('../views/class/Class'),
  },
  {
    path: '/message',
    name: 'message',
    component: () => import('../views/message/Message'),
  },
  {
    path: '/info',
    name: 'info',
    component: () => import('../views/Info'),
  },

  {
    //分类详情页
    path: '/classDetail/:name',
    name: 'classDetail',
    component: () => import('../views/class/ClassDetail'),
  },
  {
    //文章详情页
    path: '/article/:id',
    name:'articledetail',
    component: () => import('../views/home/ArticleDetail'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/admin/Login'),
  },
  {
    path: '/reg',
    name: 'reg',
    component: () => import('../views/admin/Register'),
  },
  {
    //后台管理
    path: '/manage',
    name:'manage',
    component: () => import('../views/manage/Manage'),
    beforeEnter(to,from,next) {
      if(localStorage.getItem('eleToken')) {
        next()
      }else {
        next('/login')
      }
    },
    children: [
      {
        path: 'infoshow',
        name: 'infoshow',
        component: () => import('../views/manage/menu_route/info_show/Infoshow')
      },
      {
        path: '',
        component: () => import('../views/manage/menu_route/article_manage/ArticleManage')
      },
      {
        path: 'blog',
        name: 'blog',
        component: () => import('../views/manage/menu_route/article_manage/ArticleManage')
      },
      {
        path: 'class',
        name: 'class',
        component: () => import('../views/manage/menu_route/class_manage/ClassManage')
      },
      {
        path: 'usersadmin',
        name: 'usersadmin',
        component: () => import('../views/manage/menu_route/user_manage/UsersAdmin')
      },
    ]
  },
  {
    path: "/:catchAll(.*)", // 不识别的path自动匹配404
    name: 'nofound',
    component: () => import('../components/404')
  }
]

//重复点击路由错误解决
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  // mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
// router.beforeRouteUpdate(to, from, next) {
//   this.$forceUpdate()
//   next()
// }

export default router
